package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditServiceImpl carPackageAuditService;
    @Autowired
    private BpmnInfoServiceImpl bpmnInfoService;
    @Autowired
    private RuntimeServiceImpl runtimeService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldObj = serviceItemMapper.selectByPrimaryKey(serviceItem.getId());
        // 处于上架状态的商品不能修改
        if (ServiceItem.SALESTATUS_ON.equals(oldObj.getSaleStatus())) {
            throw new BusinessException("上架服务项不能修改,请下架后重试!");
        }
        // 处于审核状态的商品不能修改
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldObj.getAuditStatus())) {
            throw new BusinessException("审核状态中,不能进行修改!");
        }
        // 如果是套餐进行了编辑,改为初始化状态
        if (ServiceItem.CARPACKAGE_YES.equals(oldObj.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        // 如果审核通过的商品进行了修改,则改为初始化状态
        if (ServiceItem.AUDITSTATUS_APPROVED.equals(oldObj.getAuditStatus())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        // serviceItem.setAuditStatus(oldObj.getAuditStatus());
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = get(id);
        // 如果不是套餐直接上架
        // 如果是套餐,只有审核通过才能上架
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())
                && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("套餐服务项需要审核通过之后才能上架");
        }
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public TablePageInfo<ServiceItem> selectAllSaleOnList(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectAllSaleOnList(qo));
    }

    @Override
    public void startAudit(Long serviceItemId, Long bpmnInfoId,Long director, Long finance, String info) {
        // 根据传入的id查询服务项,服务项状态处于初始化才能进行审核逻辑
        ServiceItem serviceItem = this.get(serviceItemId);
        // 判断serviceItem对象是否为空 或者 状态不是在初始化的情况
        if (serviceItem == null || !serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_INIT)) {
            throw new BusinessException("参数异常");
        }
        // 修改服务状态,修改为审核中
        serviceItemMapper.changAuditStatus(serviceItemId,ServiceItem.AUDITSTATUS_AUDITING);
        // 审核信息记录
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(serviceItemId);
        audit.setServiceItemInfo(info);
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        audit.setCreateTime(new Date());
        audit.setCreator(ShiroUtils.getUser().getUserName());
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setInfo(info);
        audit.setAuditorId(director);
        carPackageAuditService.save(audit);
        // 启动流程实例,以金额作为实例参数
        Map<String,Object> map = new HashMap<>();
        if (director != null) {
            map.put("director",director.toString());
        }
        if (finance != null) {
            map.put("finance",finance.toString());
        }
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        // 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), audit.getId().toString(), map);
        // 更新记录实例id
        audit.setInstanceId(processInstance.getId());
        carPackageAuditService.updateInstanceId(audit);
    }

    @Override
    public void changeAuditStatus(Long serviceItemId, Integer auditStatus) {
        serviceItemMapper.updateAuditStatus(serviceItemId,auditStatus);
    }
}
